/**
 * 判断一个对象是否是 Date 对象
 */
export const isDate = (obj: object): boolean => {
    return Object.prototype.toString.call(obj) === '[object Date]';
}

/**
 * 格式化日期时间, 默认格式为 "2023-01-01 00:00:00"
 */
export const formatDateTime = (date: Date): string => {
    const op: Intl.DateTimeFormatOptions = {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit',
        hour12: false, // 24小时制
    }
    return date.toLocaleString("en-CA", op).replace(",", "");
}

/**
 * 格式化日期, 默认格式为 "2023-01-01"
 */
export const formatDate = (date: Date): string => {
    const op: Intl.DateTimeFormatOptions = {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
    }
    return date.toLocaleString("en-CA", op);
}

/**
 * 格式化日期时间 YYYY-MM-DD HH:mm:ss.SSS
 */
export const formatDateWithMillis = (date:Date): string => {
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');
    const milliseconds = String(date.getMilliseconds()).padStart(3, '0');
    // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}.${milliseconds}`;
}
/*
* 获取当前日期,并格式化: YYYY-MM-DD
*/
export const currentDate= (): string => {
    return formatDate(new Date());
}

/**
 * 获取当前日期时间,并格式化: YYYY-MM-DD HH:mm:ss
 */
export const currentDateTime = (): string => {
    return formatDateTime(new Date());
}

/**
 * 获取当前日期时间,并格式化: YYYY-MM-DD HH:mm:ss.SSS
 */
export const currentDateTimeWithMillis = (): string => {
    return formatDateWithMillis(new Date());
}

/**
 * 根据当前传入的天树构建日期对象, 如果传递的日期不是1-31之间则反回null
 * @param year 当前需要构建的年份
 * @param month 当前需要构建的月份
 * @param day 当前需要构建的添加树
 */
export const builderDateByYearMonthDay = (year: string | number, month: string | number, day: string | number): Date | null => {
    const _year = typeof year === "string" ? Number.parseInt(year) : year;
    const _month = typeof month === "string" ? Number.parseInt(month) : month;
    const _day = typeof day === "string" ? Number.parseInt(day) : day;
    if (_year > 2059 || _year < 2000) {
        return null;
    }
    if (_month < 1 || _month > 12) {
        return null;
    }
    if (_day < 1 || _day > 31) {
        return null;
    }
    const date = new Date(_year, _month, 0);
    if (_day > date.getDate()) {
        return null;
    }
    return new Date(_year, _month - 1, _day);
}

/**
 * 根据当前传入的天树构建日期对象, 如果传递的日期不是1-31之间则反回null
 * @param month 当前需要构建的月份
 * @param day 当前需要构建的添加树
 */
export const builderDateByMonthDay = (month: string | number, day: string | number): Date | null => {
    const _month = typeof month === "string" ? Number.parseInt(month) : month;
    const _day = typeof day === "string" ? Number.parseInt(day) : day;
    if (_month < 1 || _month > 12) {
        return null;
    }
    if (_day < 1 || _day > 31) {
        return null;
    }
    const currentDate = new Date();
    // 获取当前年
    const year = currentDate.getFullYear();
    if (_day > new Date(year, _month, 0).getDate()) {
        return null;
    }
    return new Date(year, _month - 1, _day);
}

/**
 * 根据当前传入的天树构建日期对象, 如果传递的日期不是1-31之间则反回null
 * @param day 当前需要构建的添加树
 */
export const builderDateByDay = (day: string | number): Date | null => {
    const currentDay = typeof day === "string" ? Number.parseInt(day) : day;
    if (currentDay < 1 || currentDay > 31) {
        return null;
    }
    const currentDate = new Date();
    // 获取当前年份和月份
    const year = currentDate.getFullYear();
    const month = currentDate.getMonth();
    if (currentDay > new Date(year, month + 1, 0).getDate()) {
        return null;
    }
    return new Date(year, month, currentDay);
}